﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;

namespace Youz.CreditRepay.Server.PayEase
{
    public partial class PayResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                var paymentType = new Pay_PaymentTypeBLL().Get("PayEase");
                var payRecordsBLL = new Pay_RecordsBLL();

                string v_oid_ = Request["v_oid"].ToString();//订单编号
                string v_pmode_ = Request["v_pmode"].ToString();//支付方式
                string v_pstatus_ = Request["v_pstatus"].ToString(); //支付结果，1支付成功 3支付失败
                string v_pstring_ = Request["v_pstring"].ToString();//支付结果信息
                string v_count_ = Request["v_count"].ToString();//订单个数
                string v_mac_ = Request["v_mac"].ToString();//数字指纹
                string v_amount_ = Request["v_amount"].ToString();//订单金额
                string v_moneytype_ = Request["v_moneytype"].ToString();//币种
                string v_md5money_ = Request["v_md5money"].ToString();//数字指纹
                string v_sign_ = Request["v_sign"].ToString();//RSA数字指纹

                Core.WriteOperationLog("PayEase", "PayResults", v_oid_);
                
                //MD5校验
                ClassHmac2003.ClassHmac cc = new ClassHmac2003.ClassHmac();
                var mm = v_oid_ + HttpUtility.UrlEncode(v_pmode_).ToUpper() + v_pstatus_ + HttpUtility.UrlEncode(v_pstring_).ToUpper() + v_count_;
                var mac = cc.hex_hmac_md5(mm, paymentType.MerchantKey);
                var md = v_amount_ + v_moneytype_;
                var md5money = cc.hex_hmac_md5(md, paymentType.MerchantKey);

                if (mac == v_mac_ && md5money == v_md5money_)
                {
                    var arr_oid = v_oid_.Split(new String[] { "|_|" }, StringSplitOptions.None);
                    var arr_pmode = v_pmode_.Split(new String[] { "|_|" }, StringSplitOptions.None);
                    var arr_pstatus = v_pstatus_.Split(new String[] { "|_|" }, StringSplitOptions.None);
                    var arr_pstring = v_pstring_.Split(new String[] { "|_|" }, StringSplitOptions.None);
                    var arr_amount = v_amount_.Split(new String[] { "|_|" }, StringSplitOptions.None);

                    for (int i = 0; i < arr_oid.Length; i++)
                    {
                        if (string.IsNullOrEmpty(arr_oid[i])) continue;

                        string tradeNo = arr_oid[i].Split('-')[2];

                        if (string.IsNullOrEmpty(tradeNo)) continue;

                        #region Pay_PayEaseResults
                        var results = new Pay_PayEaseResults();
                        results.PaymentResultsID = Guid.NewGuid();
                        results.v_oid = arr_oid[i];
                        results.v_pmode = arr_pmode[i];
                        results.v_pstatus = arr_pstatus[i];
                        results.v_pstring = arr_pstring[i];
                        results.v_amount = arr_amount[i];
                        results.v_moneytype = v_moneytype_;
                        results.v_md5money = v_md5money_;
                        results.v_sign = v_sign_;
                        results.v_count = v_count_;
                        results.v_mac = v_mac_;
                        results.Type = 1;
                        results.md5money = md5money;
                        results.CreateTime = DateTime.Now;
                        results.Memo = tradeNo;
                        new Pay_PayEaseResultsBLL().Add(results);
                        #endregion

                        var payRecords = payRecordsBLL.Get(tradeNo);

                        //判断状态是否能支付
                        if (payRecords != null)
                        {
                            //0=待处理，1=支付成功，3=支付失败
                            if (Convert.ToInt32(arr_pstatus[i]) == 1)//支付成功
                            {
                                if (payRecords.Status != PayRecordsStatus.Payed && payRecordsBLL.UpdateStatus(payRecords.TradeNo, PayRecordsStatus.Payed))
                                {
                                    //充值
                                    var rechargeBLL = new Member_RechargeBLL();
                                    var amount = TypeHelper.ConvertTo<decimal>(arr_amount[i]);
                                    var recharge = rechargeBLL.Get(payRecords.ParentID);
                                    rechargeBLL.SaveAudit(RechargeStatus.Audited, amount, "在线支付", recharge);
                                }
                            }
                            else if (Convert.ToInt32(arr_pstatus[i]) == 3)
                            {
                                payRecordsBLL.UpdateStatus(payRecords.TradeNo, PayRecordsStatus.Close);
                            }
                        }
                    }

                    Response.Write("sent");
                }
                else
                {
                    Response.Write("error");
                }
            }
            catch (Exception ex)
            {
                Core.WriteErrorLog(ex);
                Response.Write("error");
            }
        }
    }
}